package com.lun.blog.service.impl;

import com.lun.blog.repostory.BlogRepository;
import com.lun.blog.exceptions.NotFundException;
import com.lun.blog.pojo.Blog;
import com.lun.blog.pojo.Type;
import com.lun.blog.service.BlogService;
import com.lun.blog.utils.MyBeanUtils;
import com.lun.blog.vo.BlogQuery;
import com.lun.blog.vo.SearchQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogRepository blogRepository;

    @Override
    public Blog getBlog(Long id) {
        return blogRepository.getOne(id);
    }

    @Override
    public Page<Blog> listBlog(Pageable pageable, BlogQuery blog) {

        return blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if (!"".equals(blog.getTitle()) && blog.getTitle()!=null){
                    predicates.add(criteriaBuilder.like(root.<String>get("title"),"%"+blog.getTitle()+"%"));
                }
                if(blog.getTypeId()!=null){
                    predicates.add(criteriaBuilder.equal(root.<Type>get("type").get("id"),blog.getTypeId()));
                }
                criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        },pageable);
    }


    @Override
    @Transactional
    public Blog saveBlog(Blog blog) {
        blog.setView(0);
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        return blogRepository.save(blog);
    }

    @Override
    @Transactional
    public Blog updateBlog(Long id, Blog blog) {
        Blog b= blogRepository.getOne(id);
        if(b==null){
            throw new NotFundException("博客不存在");
        }
        BeanUtils.copyProperties(blog,b, MyBeanUtils.getNotNullPropertyName(blog));
        b.setUpdateTime(new Date());
        return blogRepository.save(b);
    }

    @Override
    @Transactional
    public boolean deleteBlog(Long id) {

        Blog one = blogRepository.getOne(id);
        if(!ObjectUtils.isEmpty(one)){
            blogRepository.deleteById(id);
            return true;
        }
        return true;
    }

    @Override
    public Blog findBlogByCreateTime() {
        return blogRepository.findBlog();
    }

    @Override
    public List<Map> findCountByTypeId() {
        return blogRepository.findCountByTypeId();
    }

    @Override
    public Page<Blog> ListByPublished(Pageable pageable) {
        Page<Blog> blogs = blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                predicates.add(criteriaBuilder.equal(root.get("published"),true));
                criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
        return blogs;
    }

    @Override
    public Page<Blog> likeByBlog(SearchQuery query, Pageable pageable) {

        Page<Blog> page = blogRepository.findAll(new Specification<Blog>() {
            @Override
            public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                predicates.add(criteriaBuilder.equal(root.get("published"),true));
                if (query.getTitle() != null) {
                    predicates.add(criteriaBuilder.like(root.get("title"), "%" + query.getTitle() + "%"));
                }
                if(query.getTypeId() != null){
                    predicates.add(criteriaBuilder.equal(root.<Type>get("type").get("id"),query.getTypeId()));
                }
                criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
        return page;
    }

    @Override
    public Long countBlog() {
        return blogRepository.count();
    }

    @Override
    public Long countView() {
        return blogRepository.sumByView();
    }

    @Override
    public Blog findById(Long id) {
        return blogRepository.findById(id).get();
    }
}
